package com.epam.cdp.jpa.dao;


import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.TypedQuery;

import com.epam.cdp.jpa.model.BookCopy;

@NamedQueries({
	 @NamedQuery(name = "bookCopiesForBookByStatus", 
		     query = "SELECT bc FROM BookCopy bc WHERE bc.book.id = :bookId AND bc.available = :availability"),
	 @NamedQuery(name = "bookCopiesForBook",
	 	     query = "SELECT bc FROM BookCopy bc WHERE bc.book.id = :bookId")
})
@Stateless
public class BookCopyDAO extends AbstractStatelessDAO<BookCopy> {

    @Override
    public Class<BookCopy> getEntityClass() {
	return BookCopy.class;
    }

    public List<BookCopy> getBookCopiesByStatus(long bookId, boolean available) {
	TypedQuery<BookCopy> query = getEntityManager().createNamedQuery("bookCopiesForBookByStatus", BookCopy.class);
	query.setParameter("bookId", bookId);
	query.setParameter("availability", available);
	return query.getResultList();
    }
    
    public List<BookCopy> getBookCopiesByBookId(long bookId) {
	TypedQuery<BookCopy> query = getEntityManager().createNamedQuery("bookCopiesForBook", BookCopy.class);
	query.setParameter("bookId", bookId);
	return query.getResultList();
    }
}
